获取与Selected事件中的单元格关联的数据

时间:2016-10-08 12:57:51

标签: javascript jquery jquery-ui

我正在做一个用jquery / javascript开发的表单生成器应用程序。 我正在使用一个html表,以便可以选择单元格我正在使用jquery'selectable'api。我在初始化单元格时将一些元数据附加到单元格,现在我想在“选定”事件中访问它。但有些如何无法访问。

/**
 * Get a list of users from Active Directory.
 */
$ldap_password = 'PASSWORD';
$ldap_username = 'USERNAME@DOMAIN';
$ldap_connection = ldap_connect(HOSTNAME);
if (FALSE === $ldap_connection){
    // Uh-oh, something is wrong...
}

// We have to set this option for the version of Active Directory we are using.
ldap_set_option($ldap_connection, LDAP_OPT_PROTOCOL_VERSION, 3) or die('Unable to set LDAP protocol version');
ldap_set_option($ldap_connection, LDAP_OPT_REFERRALS, 0); // We need this for doing an LDAP search.

if (TRUE === ldap_bind($ldap_connection, $ldap_username, $ldap_password)){
    $ldap_base_dn = 'DC=XXXX,DC=XXXX';
    $search_filter = '(&(objectCategory=person)(samaccountname=*))';
    $attributes = array();
    $attributes[] = 'givenname';
    $attributes[] = 'mail';
    $attributes[] = 'samaccountname';
    $attributes[] = 'sn';
    $result = ldap_search($ldap_connection, $ldap_base_dn, $search_filter, $attributes);
    if (FALSE !== $result){
        $entries = ldap_get_entries($ldap_connection, $result);
        for ($x=0; $x<$entries['count']; $x++){
            if (!empty($entries[$x]['givenname'][0]) &&
                 !empty($entries[$x]['mail'][0]) &&
                 !empty($entries[$x]['samaccountname'][0]) &&
                 !empty($entries[$x]['sn'][0]) &&
                 'Shop' !== $entries[$x]['sn'][0] &&
                 'Account' !== $entries[$x]['sn'][0]){
                $ad_users[strtoupper(trim($entries[$x]['samaccountname'][0]))] = array('email' => strtolower(trim($entries[$x]['mail'][0])),'first_name' => trim($entries[$x]['givenname'][0]),'last_name' => trim($entries[$x]['sn'][0]));
            }
        }
    }
    ldap_unbind($ldap_connection); // Clean up after ourselves.
}

$message .= "Retrieved ". count($ad_users) ." Active Directory users\n";

ui.data在上面的代码中始终未定义。我是jquery的新手,很抱歉这么简单的问题。

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容获取所选单元格的data-element属性:

$(ui.selected).data("element")

$("#form_table").selectable({
    filter: "td",
    selected: function (event, ui) {
        console.log("Selected: " + $(ui.selected).data("element"));
    }
});
 td.ui-selecting { background: #FECA40; }
 td.ui-selected { background: #F39814; color: white; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU="   crossorigin="anonymous"></script>
<table id="form_table">
    <tr>
        <td class="ui-widget-content" data-element="elem1">Element 1</td>
        <td class="ui-widget-content" data-element="elem2">Element 2</td>
        <td class="ui-widget-content" data-element="elem3">Element 3</td>
    </tr>
    <tr>
        <td class="ui-widget-content" data-element="elem4">Element 4</td>
        <td class="ui-widget-content" data-element="elem5">Element 5</td>
        <td class="ui-widget-content" data-element="elem6">Element 6</td>
    </tr>
</table>